<?php
namespace gzapi\accept\controller;

use gzapi\common\business\Accept;
use cmf\controller\PublicController;
use think\Validate;
use gzapi\common\model\GzHiseServiceProjectModel;
use think\Db;
class ServiceController extends PublicController
{
    //方法验证参数
    protected $auth = '*';
    protected $Accept;

    protected function initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10001, 'msg' => '登录已失效!']);
        }
        $this->Accept = new Accept;
        $this->GzHiseServiceProject = new GzHiseServiceProjectModel;
    }

    //创建竣工信息
    public function acceptSave()
    {
        $get_data = input('param.');
        if(!isset($get_data['azd_id']) || !$get_data['azd_id'])
        {
            $this->error('标识错误');
        }
        //如果是战略，查找上一个功能流程是否完成，没有完成不能创建
        $azd = $this->GzHiseServiceProject->where('azd_id',$get_data['azd_id'])->find();
        $config=config('Gzflag');
        if(strpos($azd['ywbk'],$config) && $azd['debugging'] ==0)
        {
            $this->error('战略项目，请先完成调试。');
        }
        $where['azd_id'] = ['=',$get_data['azd_id']];
        $data['azd_id'] = $get_data['azd_id'];
        //创建人
        $data['created_by'] = $this->user['mobilphone'];
        $data['created_by_name'] = $this->user['user_name'];
        //服务工程师
        $data['service_engineer'] = $this->user['mobilphone'];
        $data['service_engineer_name'] = $this->user['user_name'];
        //验收单位
        $data['acceptance_unit'] = $this->user['bm_code'];
        $data['created_date'] = date('Y-m-d',time());

        $result = $this->Accept->acceptSave($where,$data);
        if($result['code'])
        {
            $this->success("创建成功!");
        }else{
            $this->error($result['msg']);
        }
    }
    //保存竣工信息
    public function acceptUpdate()
    {
        $get_data = input('param.');


        if(!isset($get_data['azd_id']) || !$get_data['azd_id'])
        {
            $this->error('标识错误');
        }
        $where['azd_id'] = ['=',$get_data['azd_id']];
        $data['azd_id'] = $get_data['azd_id'];
        //验收报告
        $data['acceptance_report'] = $get_data['acceptance_report'];
        if(!$data['acceptance_report']){
             $this->error('请上传竣工证明');
        }
        //交房时间
        if(isset($get_data['delivery_time']) && $get_data['delivery_time'])
        {
            $data['delivery_time'] = $get_data['delivery_time'];
        }
        //判断调试资料是否上传
        //$ts_data_res=Db::table('cmf_gz_hise_service_pro_test')->where('azd_id',$get_data['azd_id'])->value('ts_data_url');
        //if($ts_data_res==""){
            //$this->error('请先上传调试资料');
       // }
        //验收人员
        $data['acceptance_personnel'] = $this->user['mobilphone'];
        $data['acceptance_personnel_name'] = $this->user['user_name'];
        //修改人员
        $data['last_upd_by'] = $this->user['mobilphone'];
        $data['last_upd_by_name'] = $this->user['user_name'];
        $data['last_upd_date'] = date('Y-m-d',time());
        $result = $this->Accept->acceptSave($where,$data);
        if($result['code'])
        {
            //修改安装单主表信息状态
            $this->GzHiseServiceProject->where('azd_id',$get_data['azd_id'])->update(['gczt'=>5]);
            $this->success($result['msg']);
        }else{
            $this->error($result['msg']);
        }
    }
    //获取竣工信息
    public function getAccept()
    {
        $get_data = input('param.');
        if(!isset($get_data['azd_id']) || !$get_data['azd_id'])
        {
            $this->error('标识错误');
        }
        $where['azd_id'] = ['=',$get_data['azd_id']];
        $data = $this->Accept->getAccept($where);
        if($data)
        {
            $this->success("获取成功!",['data'=>$data]);
        }else{
//            $this->error("没有信息!",['data'=>$data]);
            return json(['code'=>20,'msg'=>'没有信息']);
        }
    }

}